package gw.com.android.utils;

import android.app.Activity;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import gw.com.android.app.ActivityManager;
import gw.com.android.ui.onlive.Comment;
import www.com.library.util.DeviceUtil;

/**
 * 处理有评论信息需要显示的需求
 * 目前：圈子，我的帖子，分析师观点，分析师点评 - 就这些地方。
 */
public class CommentsHelper {

    private static volatile CommentsHelper mInstance;//引用

    /**
     * 获取单例引用
     */
    public static CommentsHelper getInstance() {
        CommentsHelper inst = mInstance;
        if (inst == null) {
            synchronized (CommentsHelper.class) {
                inst = mInstance;
                if (inst == null) {
                    inst = new CommentsHelper();
                    mInstance = inst;
                }
            }
        }
        return inst;
    }

    /**
     * @param triangleView 三角形···
     */
    public void addComment(final Activity mContext, LinearLayout ll_comments, LayoutInflater mInflater, List<Comment> comments, ImageView triangleView) {
        SpannableStringBuilder stringBuilder;

        if (comments == null || comments.size() == 0) {
            ll_comments.setVisibility(View.GONE);
            if (null != triangleView)
                triangleView.setVisibility(View.GONE);
        } else {
            ll_comments.setVisibility(View.VISIBLE);
            if (null != triangleView)
                triangleView.setVisibility(View.VISIBLE);
        }
        //自适应增加评论
        ll_comments.removeAllViews();
        for (int i = 0; i < comments.size(); i++) {
            final Comment comment = comments.get(i);
            stringBuilder = new SpannableStringBuilder();
            String cbStr = comment.userName + "：" + comment.content;
            stringBuilder.append(cbStr);//添加文字
            ClickableSpan clickSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    PackStatis.getToService("clickTeacher2", PackStatis.EventCategory.CIRCLE.getValue(), null, null);
                    ActivityManager.showAnalystPage(comment.userId, comment.accountNo, mContext);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setColor(Color.RED);
                    ds.setUnderlineText(false);    //去除超链接的下划线
                }
            };
            stringBuilder.setSpan(clickSpan, 0, comment.userName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            //添加变色部分
            ForegroundColorSpan nameColorSpan = new ForegroundColorSpan(Color.parseColor("#467DBF"));
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#888888"));
            stringBuilder.setSpan(nameColorSpan, 0, comment.userName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            stringBuilder.setSpan(colorSpan, comment.userName.length(), comment.userName.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            TextView tv = (TextView) mInflater.inflate(R.layout.textview_comments, ll_comments, false);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
            tv.setText(stringBuilder);
            tv.setTextSize(15);
            tv.setTextColor(mContext.getResources().getColor(R.color.color_b));
            new CopyShowerUtil(mContext, tv).setOriginalColor(0xffF8F8F8);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (i != 0) {
                params.topMargin = DeviceUtil.instance().dip2px(4, mContext);
            }
            ll_comments.addView(tv, params);
        }
    }

}
